package com.learn.jdk21.dome;

import org.junit.jupiter.api.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * @author caiming01
 * @date 2023/10/22 23:47
 *
 * <a href="https://juejin.cn/post/7280747221510553654">...</a>
 */
public class VirtualThreadDemo {

    @Test
    public void testStartVirtualThread01() {
        CustomThread customThread = new CustomThread();
        Thread.startVirtualThread(customThread);
    }

    @Test
    public void testStartVirtualThread02() {
        CustomThread customThread = new CustomThread();
        // 创建不启动
        Thread unStarted = Thread.ofVirtual().unstarted(customThread);
        unStarted.start();
        // 创建直接启动
        Thread.ofVirtual().start(customThread);
    }


    @Test
    public void testStartVirtualThread03() {
        CustomThread customThread = new CustomThread();
        ThreadFactory factory = Thread.ofVirtual().factory();
        Thread thread = factory.newThread(customThread);
        thread.start();
    }

    @Test
    public void testStartVirtualThread04() {
        CustomThread customThread = new CustomThread();
        ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
        executor.submit(customThread);
    }


    static class CustomThread implements Runnable {
        @Override
        public void run() {
            System.out.println("CustomThread run");
        }

    }
}


